/**
 * Project Name:BGI_DX
 * File Name:LimitSqlExecutor.java
 * Package Name:com.bgi.dx.common.pulgin
 * Date:2014-10-20下午09:41:00
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
 */

package com.bgi.uims.common.pulgin;

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.StatementScope;

/**
 * ClassName:LimitSqlExecutor <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2014-10-20 下午09:41:00 <br/>
 * 
 * @author fukun
 * @version
 * @since JDK 1.7
 * @see
 */
public class LimitSqlExecutor extends SqlExecutor {

	private static final Log logger = LogFactory.getLog(LimitSqlExecutor.class);
	private Dialect dialect;
	private boolean enableLimit = true;

	public Dialect getDialect() {
		return dialect;
	}

	public void setDialect(Dialect dialect) {
		this.dialect = dialect;
	}

	public boolean isEnableLimit() {
		return enableLimit;
	}

	public void setEnableLimit(boolean enableLimit) {
		this.enableLimit = enableLimit;
	}

	/**
	 * 重写ibatis执行sql方法
	 * 
	 * @param statementScope
	 * @param conn
	 * @param sql
	 * @param parameters
	 * @param skipResults
	 * @param maxResults
	 * @param callback
	 * @throws SQLException
	 * @see com.ibatis.sqlmap.engine.execution.SqlExecutor#executeQuery(com.ibatis.sqlmap.engine.scope.StatementScope,
	 *      java.sql.Connection, java.lang.String, java.lang.Object[], int, int,
	 *      com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback)
	 */
	public void executeQuery(StatementScope statementScope, Connection conn,
			String sql, Object[] parameters, int skipResults, int maxResults,
			RowHandlerCallback callback) throws SQLException {
		if ((skipResults != NO_SKIPPED_RESULTS || maxResults != NO_MAXIMUM_RESULTS)
				&& supportsLimit()) {
			sql = dialect.getLimitString(sql, skipResults, maxResults);
			if (logger.isDebugEnabled()) {
				logger.debug(sql);
			}
			/** 设置skipResults为SqlExecutor不分页 设置maxResults为SqlExecutor不分页 */
			skipResults = NO_SKIPPED_RESULTS;
			maxResults = NO_MAXIMUM_RESULTS;
//			System.out.println(sql);
		}
		super.executeQuery(statementScope, conn, sql, parameters, skipResults,
				maxResults, callback);
	}

	/**
	 * @return
	 * @author 
	 */
	public boolean supportsLimit() {
		if (enableLimit && dialect != null) {
			return dialect.supportsLimit();
		}
		return false;
	}
}
